                .586
                .MMX
				.XMM
                TITLE   DELTA STREAM
                .MODEL  FLAT, STDCALL

				_MMX_ = 1
				_SSE_ = 1


INTEL_Delta_To_BitLine_Pos	PROTO	C, Delta:DWORD, BitLine:DWORD, Leng:DWORD
INTEL_Delta_To_BitLine_Neg	PROTO	C, Delta:DWORD, BitLine:DWORD, Leng:DWORD




;====================================================================
;				                                                    
;				M A C R O																
;																	
;====================================================================

_PMOVMSKB	MACRO	
		PAND	MM0,[BitMask]

		MOVQ	MM1,MM0
		PSRLQ	MM0,32
		POR		MM0,MM1

		MOVQ	MM1,MM0
		PSRLQ	MM0,16
		POR		MM0,MM1

		MOVQ	MM1,MM0
		PSRLQ	MM0,8
		POR		MM0,MM1

		MOVD	EAX,MM0
		AND		EAX,0FFh

ENDM

LabelGen	MACRO	Prefix, N
Prefix&N&	LABEL	PROC
ENDM

LabelDef	MACRO	Prefix, N
			DD		Prefix&N&		
ENDM


Write2		MACRO	N
IF		(N EQ 1)
		MOV		[EDI],AL
		INC		EDI
		
ELSEIF	(N EQ 2)
		MOV		[EDI],AH
		INC		EDI
		

ELSEIF	(N EQ 3)
		MOV		[EDI],AX
		ADD		EDI,2
ENDIF		

ENDM


Write4		MACRO	N

IF		(N EQ 0Fh)
		MOV		[EDI],EAX
		ADD		EDI,4

ELSE
		Write2	%(N and 3)
IF		(N and 0Ch)
		SHR		EAX,16
		Write2	%(N shr 2)
ENDIF
ENDIF


ENDM


Write1	MACRO	A, Msk, NEG
LOCAL	L1, L2
		CMP		A,0
		JE		L2
IFB		<NEG>	
		JL		L1
		DEC		A
ELSE
		JG		L1
		INC		A
ENDIF							
L1:		MOV		[EDI],A
		INC		EDI
		OR		BYTE PTR [EDX],Msk
L2:						
ENDM


;====================================================================
;
;				D A T A
;
;====================================================================
				.DATA
				ALIGN	8

BitMask			DQ	8040201008040201h	


JumpTable1		LABEL	DWORD
		N = 0
REPT	256
		LabelDef	JTbl1_, %(N)
		N = N+1
ENDM						

JumpTable2		LABEL	DWORD
		N = 0
REPT	256
		LabelDef	JTbl2_, %(N)
		N = N+1
ENDM						



NJumpTable1		LABEL	DWORD
		N = 0
REPT	256
		LabelDef	NJTbl1_, %(N)
		N = N+1
ENDM						

NJumpTable2		LABEL	DWORD
		N = 0
REPT	256
		LabelDef	NJTbl2_, %(N)
		N = N+1
ENDM						




;====================================================================
;
;				C O D E
;
;====================================================================
				.CODE

;___________________________________________________________________________________________________________________________
;
;				INTEL_Delta_To_BitLine_Pos
;
;		j = 0;
;		for (i = 0; i<Delta_Pnt; i++) {
;			if (QDelta[i] == 0) {
;				ClrBit(BitLine, i);
;			} else {
;				SetBit(BitLine, i);
;		        if (QDelta[i] > 0) QDelta[j] = QDelta[i] - 1; else QDelta[j] = QDelta[i];
;				j += 1;
;			}
;		}
;
;
;___________________________________________________________________________________________________________________________
INTEL_Delta_To_BitLine_Pos	PROC	C, Delta:DWORD, BitLine:DWORD, Leng:DWORD
		PUSHAD

		MOV		EAX,1
		CPUID	
		TEST	EDX,1 shl 23
IF		_MMX_
		JNE		@@Processor_supports_the_MMX
ENDIF

;__________________________________________________________________________Processor_supports_nothing:

		MOV		EDI,[Delta]
		MOV		ESI,EDI
		MOV		EDX,[BitLine]

		MOV		ECX,[Leng]
		SHR		ECX,3
		JE		@@n50

@@n5:	MOV		BYTE PTR [EDX],0

		MOV		EAX,[ESI]		
		CMP		EAX,0
		JE		@@n6
		Write1	AL, 1
		Write1	AH, 2		
		SHR		EAX,16
		JE		@@n6
		Write1	AL, 4
		Write1	AH, 8

@@n6:
		MOV		EAX,[ESI+4]		
		CMP		EAX,0
		JE		@@n7
		Write1	AL, 10h
		Write1	AH, 20h		
		SHR		EAX,16
		JE		@@n7
		Write1	AL, 40h
		Write1	AH, 80h

@@n7:	INC		EDX
		ADD		ESI,8

		DEC		ECX
		JNE		@@n5




@@n50:	MOV		ECX,[Leng]
		AND		ECX,7
		JE		@@n100

		MOV		BYTE PTR [EDX],0

IRPC	S, <0123456>
LOCAL	L1, L2		
		MOV		AL,[ESI+S]
		CMP		AL,0
		JE		L2
		JL		L1
		DEC		AL
L1:		MOV		[EDI],AL
		INC		EDI
		OR		BYTE PTR [EDX],1 shl S
L2:		DEC		ECX		
		JE		@@n100		
ENDM			

@@n100:

		SUB		EDI,[Delta]
		MOV		[Leng],EDI


		POPAD
		MOV		EAX,[Leng]

		RET

@@Processor_supports_the_MMX:


		TEST	EDX,1 shl 25
IF		_SSE_
		JNE		@@Processor_supports_the_SSE
ENDIF

;____________________________________________________________________________Processor_supports_the_MMX:

		MOV		EDI,[Delta]
		MOV		ESI,EDI
		MOV		EDX,[BitLine]

		MOV		ECX,[Leng]
		SHR		ECX,3
		JE		@@50


		PXOR	MM7,MM7

@@5_2:	MOVQ	MM0,[ESI]
		MOVQ	MM1,MM0
		MOVQ	MM2,MM0
		PCMPGTB	MM1,MM7				;; positive bytes
		PCMPEQB	MM0,MM7				;; zero bytes
		PADDB	MM2,MM1				;; positive bytes -1
		_PMOVMSKB					;; EAX,MM0			

		XOR		EAX,0FFh
		MOV		[EDX],AL
		INC		EDX

		JMP		[JumpTable2+EAX*4]

		N = 1
REPT	255
		LabelGen	JTbl2_, %N

IF		(N and 0Fh)
		MOVD	EAX,MM2
		Write4	%(N and 0Fh)		
ENDIF

IF		(N shr 4)
		PSRLQ	MM2,32
		MOVD	EAX,MM2
		Write4	%(N shr 4)		
ENDIF


IF		(N NE 255)
		JMP		JTbl2_0
ENDIF				
		N = N+1
ENDM

JTbl2_0	LABEL	PROC
		ADD		ESI,8
		DEC		ECX
		JNE		@@5_2

@@50_2:	MOV		ECX,[Leng]
		AND		ECX,7
		JE		@@100_2

		MOV		BYTE PTR [EDX],0

IRPC	S, <0123456>
LOCAL	L1, L2		
		MOV		AL,[ESI+S]
		CMP		AL,0
		JE		L2
		JL		L1
		DEC		AL
L1:		MOV		[EDI],AL
		INC		EDI
		OR		BYTE PTR [EDX],1 shl S
L2:		DEC		ECX		
		JE		@@100_2		
ENDM			

@@100_2:

		SUB		EDI,[Delta]
		MOV		[Leng],EDI


		POPAD
		MOV		EAX,[Leng]
		EMMS



		RET

@@Processor_supports_the_SSE:
;____________________________________________________________________________Processor_supports_the_SSE:

		MOV		EDI,[Delta]
		MOV		ESI,EDI
		MOV		EDX,[BitLine]

		MOV		ECX,[Leng]
		SHR		ECX,3
		JE		@@50


		PXOR	MM7,MM7

@@5:	PREFETCHNTA	[ESI+8]

		MOVQ	MM0,[ESI]
		MOVQ	MM1,MM0
		MOVQ	MM2,MM0
		PCMPGTB	MM1,MM7				;; positive bytes
		PCMPEQB	MM0,MM7				;; zero bytes
		PADDB	MM2,MM1				;; positive bytes -1
		PMOVMSKB EAX,MM0			

		XOR		EAX,0FFh
		MOV		[EDX],AL
		INC		EDX

		JMP		[JumpTable1+EAX*4]

		N = 1
REPT	255
		LabelGen	JTbl1_, %N

IF		(N and 0Fh)
		MOVD	EAX,MM2
		Write4	%(N and 0Fh)		
ENDIF

IF		(N shr 4)
		PSRLQ	MM2,32
		MOVD	EAX,MM2
		Write4	%(N shr 4)		
ENDIF


IF		(N NE 255)
		JMP		JTbl1_0
ENDIF				
		N = N+1
ENDM

JTbl1_0	LABEL	PROC
		ADD		ESI,8
		DEC		ECX
		JNE		@@5

@@50:	MOV		ECX,[Leng]
		AND		ECX,7
		JE		@@100

		MOV		BYTE PTR [EDX],0

IRPC	S, <0123456>
LOCAL	L1, L2		
		MOV		AL,[ESI+S]
		CMP		AL,0
		JE		L2
		JL		L1
		DEC		AL
L1:		MOV		[EDI],AL
		INC		EDI
		OR		BYTE PTR [EDX],1 shl S
L2:		DEC		ECX		
		JE		@@100		
ENDM			

@@100:

		SUB		EDI,[Delta]
		MOV		[Leng],EDI


		POPAD
		MOV		EAX,[Leng]
		EMMS

		RET		

INTEL_Delta_To_BitLine_Pos	ENDP



;___________________________________________________________________________________________________________________________
;
;				INTEL_Delta_To_BitLine_Neg
;
;		j = 0;
;		for (i = 0; i<Delta_Pnt; i++) {
;			if (QDelta[i] == 0) {
;				ClrBit(BitLine, i);
;			} else {
;				SetBit(BitLine, i);
;		        if (QDelta[i] > 0) QDelta[j] = QDelta[i]; else QDelta[j] = QDelta[i]+1;
;				j += 1;
;			}
;		}
;
;
;___________________________________________________________________________________________________________________________
INTEL_Delta_To_BitLine_Neg	PROC	C, Delta:DWORD, BitLine:DWORD, Leng:DWORD
		PUSHAD

		MOV		EAX,1
		CPUID	
		TEST	EDX,1 shl 23
IF		_MMX_
		JNE		@@Processor_supports_the_MMX
ENDIF

;__________________________________________________________________________Processor_supports_nothing:

		MOV		EDI,[Delta]
		MOV		ESI,EDI
		MOV		EDX,[BitLine]

		MOV		ECX,[Leng]
		SHR		ECX,3
		JE		@@n50

@@n5:	MOV		BYTE PTR [EDX],0

		MOV		EAX,[ESI]		
		CMP		EAX,0
		JE		@@n6
		Write1	AL, 1, NEG
		Write1	AH, 2, NEG		
		SHR		EAX,16
		JE		@@n6
		Write1	AL, 4, NEG
		Write1	AH, 8, NEG	

@@n6:
		MOV		EAX,[ESI+4]		
		CMP		EAX,0
		JE		@@n7
		Write1	AL, 10h, NEG
		Write1	AH, 20h, NEG		
		SHR		EAX,16
		JE		@@n7
		Write1	AL, 40h, NEG
		Write1	AH, 80h, NEG

@@n7:	INC		EDX
		ADD		ESI,8

		DEC		ECX
		JNE		@@n5




@@n50:	MOV		ECX,[Leng]
		AND		ECX,7
		JE		@@n100

		MOV		BYTE PTR [EDX],0

IRPC	S, <0123456>
LOCAL	L1, L2		
		MOV		AL,[ESI+S]
		CMP		AL,0
		JE		L2
		JG		L1
		INC		AL
L1:		MOV		[EDI],AL
		INC		EDI
		OR		BYTE PTR [EDX],1 shl S
L2:		DEC		ECX		
		JE		@@n100		
ENDM			

@@n100:

		SUB		EDI,[Delta]
		MOV		[Leng],EDI


		POPAD
		MOV		EAX,[Leng]

		RET

@@Processor_supports_the_MMX:


		TEST	EDX,1 shl 25
IF		_SSE_
		JNE		@@Processor_supports_the_SSE
ENDIF

;____________________________________________________________________________Processor_supports_the_MMX:

		MOV		EDI,[Delta]
		MOV		ESI,EDI
		MOV		EDX,[BitLine]

		MOV		ECX,[Leng]
		SHR		ECX,3
		JE		@@50


		PXOR	MM7,MM7

@@5_2:	MOVQ	MM0,[ESI]
		MOVQ	MM1,MM7
		MOVQ	MM2,MM0
		PCMPGTB	MM1,MM0				;; negative bytes
		PCMPEQB	MM0,MM7				;; zero bytes
		PSUBB	MM2,MM1				;; negative bytes +1
		_PMOVMSKB					;; EAX,MM0			

		XOR		EAX,0FFh
		MOV		[EDX],AL
		INC		EDX

		JMP		[NJumpTable2+EAX*4]

		N = 1
REPT	255
		LabelGen	NJTbl2_, %N

IF		(N and 0Fh)
		MOVD	EAX,MM2
		Write4	%(N and 0Fh)		
ENDIF

IF		(N shr 4)
		PSRLQ	MM2,32
		MOVD	EAX,MM2
		Write4	%(N shr 4)		
ENDIF


IF		(N NE 255)
		JMP		NJTbl2_0
ENDIF				
		N = N+1
ENDM

NJTbl2_0	LABEL	PROC
		ADD		ESI,8
		DEC		ECX
		JNE		@@5_2

@@50_2:	MOV		ECX,[Leng]
		AND		ECX,7
		JE		@@100_2

		MOV		BYTE PTR [EDX],0

IRPC	S, <0123456>
LOCAL	L1, L2		
		MOV		AL,[ESI+S]
		CMP		AL,0
		JE		L2

		JG		L1
		INC		AL


L1:		MOV		[EDI],AL
		INC		EDI
		OR		BYTE PTR [EDX],1 shl S
L2:		DEC		ECX		
		JE		@@100_2		
ENDM			







@@100_2:

		SUB		EDI,[Delta]
		MOV		[Leng],EDI


		POPAD
		MOV		EAX,[Leng]
		EMMS



		RET

@@Processor_supports_the_SSE:
;____________________________________________________________________________Processor_supports_the_SSE:

		MOV		EDI,[Delta]
		MOV		ESI,EDI
		MOV		EDX,[BitLine]

		MOV		ECX,[Leng]
		SHR		ECX,3
		JE		@@50


		PXOR	MM7,MM7

@@5:	PREFETCHNTA	[ESI+8]

		MOVQ	MM0,[ESI]
		MOVQ	MM1,MM7
		MOVQ	MM2,MM0
		PCMPGTB	MM1,MM0				;; negative bytes
		PCMPEQB	MM0,MM7				;; zero bytes
		PSUBB	MM2,MM1				;; negative bytes +1
		PMOVMSKB EAX,MM0			

		XOR		EAX,0FFh
		MOV		[EDX],AL
		INC		EDX

		JMP		[NJumpTable1+EAX*4]

		N = 1
REPT	255
		LabelGen	NJTbl1_, %N

IF		(N and 0Fh)
		MOVD	EAX,MM2
		Write4	%(N and 0Fh)		
ENDIF

IF		(N shr 4)
		PSRLQ	MM2,32
		MOVD	EAX,MM2
		Write4	%(N shr 4)		
ENDIF


IF		(N NE 255)
		JMP		NJTbl1_0
ENDIF				
		N = N+1
ENDM

NJTbl1_0	LABEL	PROC
		ADD		ESI,8
		DEC		ECX
		JNE		@@5

@@50:	MOV		ECX,[Leng]
		AND		ECX,7
		JE		@@100

		MOV		BYTE PTR [EDX],0

IRPC	S, <0123456>
LOCAL	L1, L2		
		MOV		AL,[ESI+S]
		CMP		AL,0
		JE		L2
		JG		L1
		INC		AL
L1:		MOV		[EDI],AL
		INC		EDI
		OR		BYTE PTR [EDX],1 shl S
L2:		DEC		ECX		
		JE		@@100		
ENDM			

@@100:

		SUB		EDI,[Delta]
		MOV		[Leng],EDI


		POPAD
		MOV		EAX,[Leng]
		EMMS

		RET		

INTEL_Delta_To_BitLine_Neg	ENDP



END
